home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / ole2book.zip / CHAP11.ZIP / CHAP11 / COSCHMOO / COSCHMOO.CPP < prev    next >
C/C++ Source or Header  |  1993-06-15  |  14KB  |  615 lines

  1. /*
  2.  * COSCHMOO.CPP
  3.  * Component Schmoo Chapter 11
  4.  *
  5.  * WinMain and CSchmooFrame implementations.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17.  
  18. #define INITGUIDS
  19. #include "coschmoo.h"
  20.  
  21.  
  22. /*
  23.  * WinMain
  24.  *
  25.  * Purpose:
  26.  *  Main entry point of application.   Should register the app class
  27.  *  if a previous instance has not done so and do any other one-time
  28.  *  initializations.
  29.  */
  30.  
  31. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pszCmdLine, int nCmdShow)
  32.     {
  33.     LPCSchmooFrame  pFR;
  34.     FRAMEINIT       fi;
  35.     WPARAM          wRet;
  36.  
  37.    #ifndef WIN32
  38.     SetMessageQueue(96);
  39.    #endif
  40.  
  41.     //Attempt to allocate and initialize the application
  42.     pFR=new CSchmooFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  43.  
  44.     fi.idsMin=IDS_FRAMEMIN;
  45.     fi.idsMax=IDS_FRAMEMAX;
  46.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  47.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  48.     fi.idStatMenuMin=ID_MENUFILE;
  49.     fi.idStatMenuMax=ID_MENUHELP;
  50.     fi.iPosWindowMenu=WINDOW_MENU;
  51.     fi.cMenus=CMENUS;
  52.  
  53.     //If we can initialize pFR, start chugging messages
  54.     if (pFR->FInit(&fi))
  55.         wRet=pFR->MessageLoop();
  56.  
  57.     delete pFR;
  58.     return wRet;
  59.     }
  60.  
  61.  
  62.  
  63.  
  64. /*
  65.  * CSchmooFrame::CSchmooFrame
  66.  * CSchmooFrame::~CSchmooFrame
  67.  *
  68.  * Constructor Parameters:
  69.  *  hInst           HINSTANCE from WinMain
  70.  *  hInstPrev       HINSTANCE from WinMain
  71.  *  pszCmdLine      LPSTR from WinMain
  72.  *  nCmdShow        int from WInMain
  73.  */
  74.  
  75. CSchmooFrame::CSchmooFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  76.     , LPSTR pszCmdLine, int nCmdShow)
  77.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  78.     {
  79.     UINT        i;
  80.  
  81.     for (i=0; i<5; i++)
  82.         m_hBmpLines[i]=NULL;
  83.  
  84.     m_fInitialized=FALSE;
  85.     return;
  86.     }
  87.  
  88.  
  89. CSchmooFrame::~CSchmooFrame(void)
  90.     {
  91.     UINT            i;
  92.  
  93.     for (i=0; i<5; i++)
  94.         {
  95.         if (NULL!=m_hBmpLines[i])
  96.             DeleteObject(m_hBmpLines[i]);
  97.         }
  98.  
  99.     //CHAPTER8MOD
  100.     OleFlushClipboard();
  101.     //End CHAPTER8MOD
  102.  
  103.     if (m_fInitialized)
  104.         CoUninitialize();
  105.  
  106.     return;
  107.     }
  108.  
  109.  
  110.  
  111.  
  112.  
  113. /*
  114.  * CSchmooFrame::FInit
  115.  *
  116.  * Purpose:
  117.  *  Call CoInitialize then calling down into the base class
  118.  *  initialization.
  119.  *
  120.  * Parameters:
  121.  *  pFI             LPFRAMEINIT containing initialization parameters.
  122.  *
  123.  * Return Value:
  124.  *  BOOL            TRUE if initialization succeeded, FALSE otherwise.
  125.  */
  126.  
  127. BOOL CSchmooFrame::FInit(LPFRAMEINIT pFI)
  128.     {
  129.     DWORD       dwVer;
  130.  
  131.     dwVer=OleBuildVersion();
  132.  
  133.     if (rmm!=HIWORD(dwVer))
  134.         return FALSE;
  135.  
  136.     if (FAILED(OleInitialize(NULL)))
  137.         return FALSE;
  138.  
  139.     m_fInitialized=TRUE;
  140.  
  141.     return CFrame::FInit(pFI);
  142.     }
  143.  
  144.  
  145.  
  146.  
  147.  
  148. /*
  149.  * CSchmooFrame::CreateCClient
  150.  *
  151.  * Purpose:
  152.  *  Constructs a new client specific to the application.
  153.  *
  154.  * Parameters:
  155.  *  None
  156.  *
  157.  * Return Value:
  158.  *  LPCClient       Pointer to the new client object.
  159.  */
  160.  
  161. LPCClient CSchmooFrame::CreateCClient(void)
  162.     {
  163.     return (LPCClient)(new CSchmooClient(m_hInst));
  164.     }
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. /*
  172.  * CSchmooFrame::FPreShowInit
  173.  *
  174.  * Purpose:
  175.  *  Called from FInit before intially showing the window.  We do whatever
  176.  *  else we want here, modifying nCmdShow as necessary which affects
  177.  *  ShowWindow in FInit.
  178.  *
  179.  * Parameters:
  180.  *  None
  181.  *
  182.  * Return Value:
  183.  *  BOOL            TRUE if this initialization succeeded, FALSE otherwise.
  184.  */
  185.  
  186. BOOL CSchmooFrame::FPreShowInit(void)
  187.     {
  188.     CreateLineMenu();
  189.     CheckLineSelection(IDM_LINESOLID);
  190.     m_pGB->Check(IDM_LINESOLID, TRUE);
  191.     return TRUE;
  192.     }
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. /*
  200.  * CSchmooFrame::CreateLineMenu
  201.  *
  202.  * Purpose:
  203.  *  Initializes the bitmaps used to create the Line menu and replaces
  204.  *  the text items defined in the application resources with these
  205.  *  bitmaps.  Note that the contents of m_hBmpLines must be cleaned
  206.  *  up when the application terminates.
  207.  *
  208.  * Parameters:
  209.  *  None
  210.  *
  211.  * Return Value:
  212.  *  None
  213.  */
  214.  
  215. void CSchmooFrame::CreateLineMenu(void)
  216.     {
  217.     HMENU       hMenu;
  218.     HDC         hDC, hMemDC;
  219.     HPEN        hPen;
  220.     HGDIOBJ     hObj;
  221.     TEXTMETRIC  tm;
  222.     UINT        i, cx, cy;
  223.  
  224.  
  225.     hMenu=GetSubMenu(GetMenu(m_hWnd), 3);   //Line menu.
  226.     hDC=GetDC(m_hWnd);
  227.  
  228.     //Create each line in a menu item 8 chars wide, one char high.
  229.     GetTextMetrics(hDC, &tm);
  230.     cx=tm.tmAveCharWidth*8;
  231.     cy=tm.tmHeight;
  232.  
  233.     //Create a memory DC in which to draw lines, and bitmaps for each line.
  234.     hMemDC=CreateCompatibleDC(hDC);
  235.     ReleaseDC(m_hWnd, hDC);
  236.  
  237.     for (i=0; i<5; i++)
  238.         {
  239.         m_hBmpLines[i]=CreateCompatibleBitmap(hMemDC, cx, cy);
  240.         SelectObject(hMemDC, m_hBmpLines[i]);
  241.  
  242.         PatBlt(hMemDC, 0, 0, cx, cy, WHITENESS);
  243.  
  244.         hPen=CreatePen(i, 1, 0L);           //i==line style like PS_SOLID
  245.         hObj=SelectObject(hMemDC, hPen);
  246.  
  247.         MoveTo(hMemDC, 0,  cy/2);
  248.         LineTo(hMemDC, cx, cy/2);
  249.  
  250.         ModifyMenu(hMenu, IDM_LINEMIN+i, MF_BYCOMMAND | MF_BITMAP
  251.             , IDM_LINEMIN+i, (LPCSTR)MAKELONG(m_hBmpLines[i], 0));
  252.  
  253.         SelectObject(hMemDC, hObj);
  254.         DeleteObject(hPen);
  255.         }
  256.  
  257.     CheckMenuItem(hMenu, IDM_LINESOLID, MF_CHECKED);
  258.     DeleteDC(hMemDC);
  259.  
  260.     return;
  261.     }
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271. /*
  272.  * CSchmooFrame::CreateGizmos
  273.  *
  274.  * Purpose:
  275.  *  Procedure to create all the necessary gizmobar buttons.
  276.  *
  277.  * Parameters:
  278.  *  None
  279.  *
  280.  * Return Value:
  281.  *  UINT            Number of gizmos added to the bar.
  282.  */
  283.  
  284. UINT CSchmooFrame::CreateGizmos(void)
  285.     {
  286.     UINT            iLast;
  287.     UINT            uState=GIZMO_NORMAL;
  288.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  289.     UINT            utEx  =GIZMOTYPE_BUTTONATTRIBUTEEX;
  290.  
  291.     //Insert the standard ones.
  292.     iLast=CFrame::CreateGizmos();
  293.  
  294.     //Insert File Import in the 5th position and account for it in iLast.
  295.     m_pGB->Add(utCmd, 4, IDM_FILEIMPORT, m_dxB, m_dyB, NULL, m_hBmp, 2, uState);
  296.     iLast++;
  297.  
  298.     //Separator
  299.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  300.  
  301.     //For the Background bitmap, preserve our use of black (part of the image)
  302.     m_pGB->Add(utCmd, iLast++, IDM_COLORBACKGROUND, m_dxB, m_dyB, NULL, m_hBmp, 3
  303.                , GIZMO_NORMAL | PRESERVE_BLACK);
  304.  
  305.     m_pGB->Add(utCmd, iLast++, IDM_COLORLINE, m_dxB, m_dyB, NULL, m_hBmp, 4, uState);
  306.  
  307.     //Separator
  308.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  309.  
  310.     //Line styles.
  311.     m_pGB->Add(utEx, iLast++, IDM_LINESOLID,      m_dxB, m_dyB, NULL, m_hBmp, 5, uState);
  312.     m_pGB->Add(utEx, iLast++, IDM_LINEDASH,       m_dxB, m_dyB, NULL, m_hBmp, 6, uState);
  313.     m_pGB->Add(utEx, iLast++, IDM_LINEDOT,        m_dxB, m_dyB, NULL, m_hBmp, 7, uState);
  314.     m_pGB->Add(utEx, iLast++, IDM_LINEDASHDOT,    m_dxB, m_dyB, NULL, m_hBmp, 8, uState);
  315.     m_pGB->Add(utEx, iLast++, IDM_LINEDASHDOTDOT, m_dxB, m_dyB, NULL, m_hBmp, 9, uState);
  316.  
  317.     return iLast;
  318.     }
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327. /*
  328.  * CSchmooFrame::OnCommand
  329.  *
  330.  * Purpose:
  331.  *  WM_COMMAND handler for the Schmoo frame window that just processes
  332.  *  the line menu and the color menu leaving the CFrame to do everything
  333.  *  else.
  334.  *
  335.  * Parameters:
  336.  *  hWnd            HWND of the frame window.
  337.  *  wParam          WPARAM of the message.
  338.  *  lParam          LPARAM of the message.
  339.  *
  340.  * Return Value:
  341.  *  LRESULT         Return value for the message.
  342.  */
  343.  
  344. LRESULT CSchmooFrame::OnCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
  345.     {
  346.     LPCSchmooDoc    pDoc;
  347.     char            szFile[CCHPATHMAX];
  348.     BOOL            fOK;
  349.     UINT            i, uTemp;
  350.     COLORREF        rgColors[16];
  351.     CHOOSECOLOR     cc;
  352.  
  353.     COMMANDPARAMS(wID, wCode, hWndMsg);
  354.  
  355.     /*
  356.      * Don't bother with anything during first initialization,
  357.      * skipping many GizmoBar notifications.
  358.      */
  359.     if (m_fInit)
  360.         return 0L;
  361.  
  362.     pDoc=(LPCSchmooDoc)m_pCL->ActiveDocument();
  363.  
  364.     /*
  365.      * Check for the line style commands which are IDM_LINEMIN+<style>.
  366.      * We handle this by changing the menu and toolbar, then we pass
  367.      * it to the document for real processing.
  368.      */
  369.     if (NULL!=pDoc && IDM_LINEMIN <= wID && IDM_LINEMAX >=wID)
  370.         {
  371.         CheckLineSelection(wID);
  372.         pDoc->LineStyleSet(wID-IDM_LINEMIN);
  373.         return 0L;
  374.         }
  375.  
  376.     switch (wID)
  377.         {
  378.         case IDM_FILEIMPORT:
  379.             szFile[0]=0;
  380.             fOK=FSaveOpenDialog(szFile, CCHPATHMAX, IDS_FILEIMPORT, TRUE, &i);
  381.  
  382.             if (fOK)
  383.                 {
  384.                 uTemp=pDoc->ULoad(FALSE, szFile);
  385.                 pDoc->ErrorMessage(uTemp);
  386.                 }
  387.  
  388.             return (LRESULT)fOK;
  389.  
  390.  
  391.         case IDM_COLORBACKGROUND:
  392.         case IDM_COLORLINE:
  393.             //Invoke the color chooser for either color
  394.             uTemp=(IDM_COLORBACKGROUND==wID)
  395.                 ? DOCCOLOR_BACKGROUND : DOCCOLOR_LINE;
  396.  
  397.             for (i=0; i<16; i++)
  398.                 rgColors[i]=RGB(0, 0, i*16);
  399.  
  400.             memset(&cc, 0, sizeof(CHOOSECOLOR));
  401.             cc.lStructSize=sizeof(CHOOSECOLOR);
  402.             cc.lpCustColors=rgColors;
  403.             cc.hwndOwner=hWnd;
  404.             cc.Flags=CC_RGBINIT;
  405.             cc.rgbResult=pDoc->ColorGet(uTemp);
  406.  
  407.             if (ChooseColor(&cc))
  408.                 pDoc->ColorSet(uTemp, cc.rgbResult);
  409.  
  410.             break;
  411.  
  412.  
  413.         default:
  414.            CFrame::OnCommand(hWnd, wParam, lParam);
  415.         }
  416.  
  417.     return 0L;
  418.     }
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425. /*
  426.  * CSchmooFrame::OnDocumentDataChange
  427.  *
  428.  * Purpose:
  429.  *  Update the Line menu and GizmoBar if the style in the data changes.
  430.  *
  431.  * Parameters:
  432.  *  pDoc            LPCDocument notifying the sink.
  433.  *
  434.  * Return Value:
  435.  *  None
  436.  */
  437.  
  438. void CSchmooFrame::OnDocumentDataChange(LPCDocument pDoc)
  439.     {
  440.     CheckLineSelection(IDM_LINEMIN+((LPCSchmooDoc)pDoc)->LineStyleGet());
  441.     return;
  442.     }
  443.  
  444.  
  445.  
  446.  
  447. /*
  448.  * CSchmooFrame::OnDocumentActivate
  449.  *
  450.  * Purpose:
  451.  *  Informs us that document activation changed, so update the UI for
  452.  *  that new document.
  453.  *
  454.  * Parameters:
  455.  *  pDoc            LPCDocument notifying the sink.
  456.  *
  457.  * Return Value:
  458.  *  None
  459.  */
  460.  
  461. void CSchmooFrame::OnDocumentActivate(LPCDocument pDoc)
  462.     {
  463.     CheckLineSelection(IDM_LINEMIN+((LPCSchmooDoc)pDoc)->LineStyleGet());
  464.     return;
  465.     }
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473. /*
  474.  * CSchmooFrame::UpdateMenus
  475.  *
  476.  * Purpose:
  477.  *  Handles the WM_INITMENU message for the frame window.  Depending
  478.  *  on the existence of an active window, menu items are selectively
  479.  *  enabled and disabled.
  480.  *
  481.  * Parameters:
  482.  *  hMenu           HMENU of the menu to intialize
  483.  *  iMenu           UINT position of the menu.
  484.  *
  485.  * Return Value:
  486.  *  None
  487.  */
  488.  
  489. void CSchmooFrame::UpdateMenus(HMENU hMenu, UINT iMenu)
  490.     {
  491.     LPCDocument pDoc;
  492.     BOOL        fOK=FALSE;
  493.     BOOL        fCallDefault=TRUE;
  494.     UINT        i;
  495.     UINT        uTemp;
  496.     UINT        uTempE;
  497.     UINT        uTempD;
  498.  
  499.     pDoc=m_pCL->ActiveDocument();
  500.  
  501.     uTempE=MF_ENABLED | MF_BYCOMMAND;
  502.     uTempD=MF_DISABLED | MF_GRAYED | MF_BYCOMMAND;
  503.     uTemp=((NULL!=pDoc) ? uTempE : uTempD);
  504.  
  505.     //File menu:  If there is no current document window, disable Import.
  506.     if (m_phMenu[0]==hMenu)
  507.         EnableMenuItem(hMenu, IDM_FILEIMPORT, uTemp);
  508.  
  509.     //Color menu:  no document, no commands
  510.     if (m_phMenu[2]==hMenu)
  511.         {
  512.         EnableMenuItem(hMenu, IDM_COLORBACKGROUND, uTemp);
  513.         EnableMenuItem(hMenu, IDM_COLORLINE,       uTemp);
  514.         fCallDefault=FALSE;
  515.         }
  516.  
  517.     //Line menu:  no document, no commands
  518.     if (m_phMenu[3]==hMenu)
  519.         {
  520.         for (i=IDM_LINEMIN; i<=IDM_LINEMAX; i++)
  521.             EnableMenuItem(hMenu, i, uTemp);
  522.  
  523.         fCallDefault=FALSE;
  524.         }
  525.  
  526.     if (fCallDefault)
  527.         CFrame::UpdateMenus(hMenu, iMenu);
  528.  
  529.     return;
  530.     }
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537. /*
  538.  * CSchmooFrame::UpdateGizmos
  539.  *
  540.  * Purpose:
  541.  *  Enables and disables gizmos depending on whether we have
  542.  *  a document or not.
  543.  *
  544.  * Parameters:
  545.  *  None
  546.  *
  547.  * Return Value:
  548.  *  None
  549.  */
  550.  
  551. void CSchmooFrame::UpdateGizmos(void)
  552.     {
  553.     LPCDocument pDoc;
  554.     BOOL        fEnable;
  555.     UINT        i;
  556.  
  557.     //Let the default hack on its gizmos.
  558.     CFrame::UpdateGizmos();
  559.  
  560.     pDoc=m_pCL->ActiveDocument();
  561.     fEnable=(NULL!=pDoc);
  562.  
  563.     //No document, disable just about everything
  564.     m_pGB->Enable(IDM_FILEIMPORT, fEnable);
  565.  
  566.     m_pGB->Enable(IDM_COLORBACKGROUND, fEnable);
  567.     m_pGB->Enable(IDM_COLORLINE,       fEnable);
  568.  
  569.     for (i=IDM_LINEMIN; i <= IDM_LINEMAX; i++)
  570.         m_pGB->Enable(i, fEnable);
  571.  
  572.     return;
  573.     }
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580. /*
  581.  * CSchmooFrame::CheckLineSelection
  582.  *
  583.  * Purpose:
  584.  *  Maintains the bitmap menu and the gizmos for the line selection.  Both
  585.  *  are mutially exclusive option lists where a selection in one has to
  586.  *  affect the other.
  587.  *
  588.  * Parameters:
  589.  *  uID             UINT ID of the item to be selected
  590.  *
  591.  * Return Value:
  592.  *  None
  593.  */
  594.  
  595. void CSchmooFrame::CheckLineSelection(UINT uID)
  596.     {
  597.     UINT        i;
  598.     HMENU       hMenu;
  599.     LPCDocument pDoc;
  600.  
  601.     hMenu=GetMenu(m_hWnd);
  602.  
  603.     //Uncheck all lines initially.
  604.     for (i=IDM_LINEMIN; i<=IDM_LINEMAX; i++)
  605.         CheckMenuItem(hMenu, i, MF_UNCHECKED | MF_BYCOMMAND);
  606.  
  607.     CheckMenuItem(hMenu, uID, MF_CHECKED | MF_BYCOMMAND);
  608.     m_pGB->Check(uID, TRUE);
  609.  
  610.     pDoc=m_pCL->ActiveDocument();
  611.     m_pGB->Enable(uID, (NULL!=pDoc));
  612.  
  613.     return;
  614.     }
  615.